package com.gitee.feizns.v8.rander.table;

import com.gitee.feizns.StringUtils;
import lombok.Data;

import java.util.List;

/**
 * @author feizns
 * @since 2019/8/13
 */
@Data
public class Table {

    /**
     * 路由
     */
    private String api;

    /**
     * 备注
     */
    private String comment;

    /**
     * 表名
     */
    private String tableName;

    /**
     * pascal 命名
     */
    private String javaClassName;

    /**
     * 字段列表
     */
    private List<Field> fields;

    public String getJavaClassName() {
        return javaClassName == null ? StringUtils.underlineToPascal(tableName) : javaClassName;
    }

    public String getFieldName() {
        return StringUtils.underlineToHump(tableName);
    }

    public Field getPk() {
        for (Field field : fields) {
            if ( field.isPk() )
                return field;
        }
        return null;
    }

    public boolean isLast(Field field) {
        return !fields.isEmpty() && fields.get(fields.size() - 1) == field;
    }

    public String getApi() {
        return api == null ? "/" + StringUtils.toUnderline(tableName).replace("_", "/") : api;
    }

    public String getTableNamePackage() {
        return String.join(".", StringUtils.toUnderline(tableName).split("_"));
    }

}
